package ch.fhnw.qualiscan.application.controllers;

import java.util.Date;
import java.util.List;

import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.RoleModel;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.application.views.EditSurveyView;
import ch.fhnw.qualiscan.application.views.EditSurveyCoordinatorView;
import ch.fhnw.qualiscan.core.Controller;
import ch.fhnw.qualiscan.core.db.helpers.participantsTableArgs;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.helpers.Permissions;
import ch.fhnw.qualiscan.core.helpers.StageHelper;
import ch.fhnw.qualiscan.core.helpers.UserSession;


public class EditSurvey extends Controller {
	
	public SurveyModel surveyModel;
	public DomainModel domainModel;
	public UserModel userModel;
	public RoleModel roleModel;
	public StageHelper stageHelper;
	
	
	private SurveyUserRole surveyUserRole;
	public Survey currentSurvey;
	
	public Survey survey;
	public BeanItem<Survey> surveyItem;
	public Date surveyDate;
	
	public BeanItemContainer<participantsTableArgs> tableContainer;
	
	
	public void getParticipantsTableContainer() {
		
		tableContainer = new BeanItemContainer<participantsTableArgs>(participantsTableArgs.class);
		List<SurveyUserRole> surList = surveyModel.getSurListForSurvey(currentSurvey);
		for(int i = 0; i < surList.size(); i++) {
			if (surList.get(i).getUser().getId() == userSession.getId() && surList.get(i).getRole().getId() == Config.COORDINATOR_ID) {}
			else {
				String role = userModel.getRoleText(surList.get(i).getRole(), userSession.getLanguage()).getTitle();
				participantsTableArgs pta = new participantsTableArgs(surList.get(i), surList.get(i).getUser().getFirstname(), surList.get(i).getUser().getLastname(),surList.get(i).getUser().getEmail(), role);
				tableContainer.addBean(pta);
			}
		}
	}
	
	public void getSurvey() {
		survey = (Survey) hibernateSession.getSession().get(Survey.class, currentSurvey.getId());
		surveyDate = survey.getCompletiondate();
		
		surveyItem = new BeanItem<Survey>(survey);
	}
	
	public void rebuildLayout() {
		view.removeAllComponents();
		view.tabSheet.removeAllComponents();
		view.buildLayout();
		this.loadView();
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setView() {
		view = new EditSurveyView(this);
		
	}
	
	@Override
	public void loadModels() {
		surveyModel = new SurveyModel();
		domainModel = new DomainModel();
		userModel = new UserModel();
		stageHelper = new StageHelper();
		roleModel = new RoleModel();
		
	}
	
	public UserSession getUserSession() {
		return this.userSession;
	}
	@Override
	public void setNeededPermissions() {
		neededPermissions.add(Permissions.LOGGEDIN);
		
	}
	
	

	@Override
	public void urlFragmentChanged(int fragmentId, String fragment) {
		if (fragmentId == 2) {
			int surId = -1;
			
			try {
				surId = Integer.parseInt(fragment);
			}
			catch(Exception e) {
				
			}
			if (surId != -1) {
				surveyUserRole = surveyModel.getSurById(surId);
				if (surveyUserRole != null) {
					currentSurvey = surveyUserRole.getSurvey();
					if (currentSurvey == null) {
						this.getAppData().getLoaderLib().setUrl("PermissionDenied/0");
					}
					else if (surveyUserRole.getUser().getId() != userSession.getId()) {
						this.getAppData().getLoaderLib().setUrl("PermissionDenied/0");
					}
					else {
						
						if (surveyUserRole.getRole().getId() == Config.COORDINATOR_ID) {
							view = new EditSurveyCoordinatorView(this);
						}
						else {
							view = new EditSurveyView(this);
						}
						
						view.removeAllComponents();
						view.tabSheet.removeAllComponents();
						view.buildLayout();
						this.loadView();
						
					}
				}
				else {
					this.getAppData().getLoaderLib().setUrl("PermissionDenied/0");
				}
			}
		}
		
	}

	

	public Survey getCurrentSurvey() {
		return currentSurvey;
	}

	public SurveyUserRole getSurveyUserRole() {
		return surveyUserRole;
	}
	
	


}
